﻿using System;
using System.Data;
using System.Data.SqlClient;
using DevMaker.SIC.Model;
using log4net;

namespace DevMaker.SIC.Dao {
    public class CierreDetalleDao {
        private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public CierreDetalle Crear(Turno turno, Cierre cierre, CierreDetalle cierreDetalle, SqlTransaction transaction) {
            const string storeName = "sp_cierre_turnos_detalles_c";
            try {
                var command = new SqlCommand(storeName, transaction.Connection) { Transaction = transaction, CommandType = CommandType.StoredProcedure };
                command.Parameters.AddWithValue("@id_sucursal", turno.Sucursal.Id);
                command.Parameters.AddWithValue("@cod_sucursal", turno.Sucursal.Codigo);
                command.Parameters.AddWithValue("@id_cierre", cierre.Id);
                command.Parameters.AddWithValue("@id_producto", cierreDetalle.Producto.Id);
                command.Parameters.AddWithValue("@cantidad_real", cierreDetalle.CantidadFisicaEnCaja);
                command.Parameters.AddWithValue("@cantidad_vendida", cierreDetalle.CantidadVendida);
                command.Parameters.AddWithValue("@cantidad_entregada", cierreDetalle.CantidadEntregada);
                command.Parameters.AddWithValue("@precio_unitario", cierreDetalle.Producto.Precio);

                var outParam = command.Parameters.Add("@id", SqlDbType.BigInt);
                outParam.Direction = ParameterDirection.Output;
                command.ExecuteScalar();
                cierreDetalle.Id = Convert.ToInt32(outParam.Value);
            } catch (Exception ex) {
                Log.Error(ex);
                throw;
            }
            return cierreDetalle;
        }
    }
}
